package org.jetnuke.modules.pictures;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PictureUserSettingsDAO;
import org.jetnuke.entities.PictureUserSettings;
import org.jetnuke.modules.pictures.utils.PictureUtils;

public class User implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(User.class);

	int id;

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (!PictureUtils.isPictureInitialized(id)) {

			ActionForward returnActionForward = new ActionForward(info,
					"no_pictures.vm");
			if (logger.isDebugEnabled()) {
				logger
						.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
			}
			return returnActionForward;
		}
		net.jforum.entities.User usr = DataAccessDriver.getInst().newUserDAO()
				.selectById(id);
		PictureUserSettingsDAO pusd = DataAccessDriver.getInst()
				.newPictureUserSettingsDAO();
		PictureUserSettings pus = pusd.retrieveByPk(usr.getId());

		if (ui != null && ui.getID() == id) {
			vc.put("my_pictures", Boolean.TRUE);
		}
		vc.put("settings", pus);
		List preLeftBlocks = new ArrayList();
		List albums = DataAccessDriver.getInst().newPictureAlbumDAO()
				.selectByUser(id);
		PictureUtils.manageStyle(pus, vc, preLeftBlocks);
		UserDAO um = DataAccessDriver.getInst().newUserDAO();
		net.jforum.entities.User user = um.selectById(id);
		vc.put("user", user);
		vc.put("user_albums", albums);

		preLeftBlocks.add(PictureUtils.createUserBlockInfo(vc, user));

		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

	public void setId(int id) {
		this.id = id;
	}

}
